﻿using System;
using System.Collections.Generic;
using System.Text;
using He.Common;
using System.Data;

namespace He.Web.UI
{
    public class AdHelper
    {
        /// <summary>
        /// 焦点图轮播
        /// </summary>
        /// <param name="call_mark"></param>
        /// <returns></returns>
        public string GetFocusHtml(string call_mark)
        {
            BLL.ad bll = new BLL.ad();
            DataTable dt = bll.GetListByMark("is_red=1 and call_mark='" + call_mark + "'").Tables[0];

            He.Model.ad_position mposition = new He.BLL.ad_position().GetModelByMark(call_mark);

            if (mposition.ad_status == 0)
            {
                return string.Empty;
            }
            // MyFocus
            if (mposition.display_effect.StartsWith("mF"))
            {
                return MyFocusHtml(mposition.display_effect, mposition.ad_width, mposition.ad_height, mposition.ad_target, dt);
            }
            //单图
            if (mposition.display_effect == Enums.FocusEffect.单图.ToString())
            {
                return SingImgHtml(mposition.ad_width, mposition.ad_height, mposition.ad_target, dt);
            }
            //默认轮播
            if (mposition.display_effect == Enums.FocusEffect.渐变.ToString())
            {
                return DefaultFocusHtml(mposition.ad_width, mposition.ad_height, dt);
            }
            //立体3D翻转
            if (mposition.display_effect == Enums.FocusEffect.立体3D翻转.ToString())
            {
                return Cu3erHtml(mposition.ad_width, mposition.ad_height);
            }
            //左右切换
            if (mposition.display_effect == Enums.FocusEffect.左右切换.ToString())
            {
                return KinslideshowHtml(mposition.ad_width, mposition.ad_height, mposition.ad_target, dt);
            }
            //Slider
            if (mposition.display_effect == Enums.FocusEffect.Slider.ToString())
            {
                return SliderHtml(mposition.ad_width, mposition.ad_height, mposition.ad_target, dt);
            }
            //KinMaxShow
            if (mposition.display_effect == Enums.FocusEffect.KinMaxShow全屏背景.ToString())
            {
                return KinmaxshowHtml(mposition.ad_height, mposition.ad_target, dt);
            }
            //Iscroll
            if (mposition.display_effect == Enums.FocusEffect.Iscroll.ToString())
            {
                return IscrollHtml(mposition.ad_width,mposition.ad_height, mposition.ad_target, dt);
            }
            else
            {
                return DefaultFocusHtml(mposition.ad_width, mposition.ad_height, dt);
            }
        }

        /// <summary>
        /// MyFocus焦点图HTML
        /// </summary>
        /// <param name="effect"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string MyFocusHtml(string effect, string width, string height, string target, DataTable dt)
        {
            StringBuilder strTemp = new StringBuilder();
            strTemp.Append("<script type=\"text/javascript\" src=\"/plug/myfocus/js/myfocus-2.0.4.min.js\"></script>\r\n");
            strTemp.Append("<style type=\"text/css\">#myFocus{ width:" + width + "px; height:" + height + "px;}</style>\r\n");
            strTemp.Append("<script type=\"text/javascript\">\r\n");
            strTemp.Append("myFocus.set({\r\n");
            strTemp.Append("id: 'myFocus',\r\n");
            strTemp.Append("pattern: '" + effect + "',\r\n");
            strTemp.Append("trigger: 'mouseover',\r\n");
            strTemp.Append("txtHeight: '0'\r\n");
            strTemp.Append("});\r\n");
            strTemp.Append("</script>\r\n");
            strTemp.Append("<div id=\"myFocus\">\r\n");
            strTemp.Append("<div class=\"pic\">\r\n");
            strTemp.Append("<ul>\r\n");

            foreach (DataRow row in dt.Rows)
            {
                strTemp.Append("<li><a href=\"" + row["link_url"] + "\" target=\"" + target + "\"><img src=\"" + row["img_url"] + "\" thumb=\"\" alt=\"\" text=\"\" /></a></li>\r\n");
            }

            strTemp.Append("</ul>\r\n");
            strTemp.Append("</div>\r\n");
            strTemp.Append("</div>\r\n");
            return strTemp.ToString();
        }
        /// <summary>
        /// 默认焦点图HTML
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string DefaultFocusHtml(string width, string height, DataTable dt)
        {

            StringBuilder imgs = new StringBuilder();
            StringBuilder linkurl = new StringBuilder();
            foreach (DataRow row in dt.Rows)
            {
                imgs.Append(row["img_url"].ToString());
                imgs.Append("|");

                linkurl.Append(row["link_url"].ToString());
                linkurl.Append("|");
            }
            string Imgs = imgs.ToString().Trim('|');
            string LinkUrl = linkurl.ToString().Trim('|');
            return DefaultFocusHtml(width, height, Imgs, LinkUrl);
        }

        /// <summary>
        /// 默认焦点图HTML
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="imgs"></param>
        /// <param name="urls"></param>
        /// <returns></returns>
        public string DefaultFocusHtml(string width, string height, string imgs, string urls)
        {
            StringBuilder strTemp = new StringBuilder();
            strTemp.Append("<script type=\"text/javascript\">\r\n");
            strTemp.Append("<!--\r\n");
            strTemp.Append("var swf_width='" + width + "';\r\n");
            strTemp.Append("var swf_height='" + height + "';\r\n");
            strTemp.Append("var files='" + imgs + "';\r\n");
            strTemp.Append("var links='" + urls + "';\r\n");
            strTemp.Append("var texts='|||' ;\r\n");
            strTemp.Append("document.write('<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\" http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"'+ swf_width +'\" height=\"'+ swf_height +'\">');\r\n");
            strTemp.Append("document.write('<param name=\"movie\" value=\"/swf/Foucs1.swf\"><param name=\"quality\" value=\"high\">');\r\n");
            strTemp.Append("document.write('<param name=\"menu\" value=\"false\"><param name=wmode value=\"opaque\">');\r\n");
            strTemp.Append("document.write('<param name=\"FlashVars\" value=\"bcastr_file='+files+'&bcastr_link='+links+'&bcastr_title='+texts+'\">');\r\n");
            strTemp.Append("document.write('<embed src=\"/swf/Foucs1.swf\" wmode=\"opaque\" FlashVars=\"bcastr_file='+files+'&bcastr_link='+links+'&bcastr_title='+texts+'& menu=\"false\" quality=\"high\" width=\"'+ swf_width +'\" height=\"'+ swf_height +'\" type=\"application/x-shockwave-flash\" pluginspage=\" http://www.macromedia.com/go/getflashplayer\" />');\r\n");
            strTemp.Append("document.write('</object>');\r\n");
            strTemp.Append("//-->\r\n");
            strTemp.Append("</script>\r\n");
            return strTemp.ToString();
        }

        /// <summary>
        /// 立体3D翻转HTML
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public string Cu3erHtml(string width, string height)
        {
            StringBuilder strTemp = new StringBuilder();

            strTemp.Append("<script type=\"text/javascript\" src=\"/js/swfobject.js\"></script>\r\n");
            strTemp.Append("<script type=\"text/javascript\">\r\n");
            strTemp.Append("var flashvars = {};\r\n");
            strTemp.Append("flashvars.xml = \"config.xml\"\r\n");
            strTemp.Append("var params = {}\r\n");
            strTemp.Append("params.allowscriptaccess = \"always\"\r\n");
            strTemp.Append("params.allownetworking = \"all\"\r\n");
            strTemp.Append("params.wmode = \"Transparent\"\r\n");
            strTemp.Append("var attributes = {}\r\n");
            strTemp.Append("attributes.id = \"slider\"\r\n");
            strTemp.Append("attributes.wmode = \"Transparent\"\r\n");
            strTemp.Append("swfobject.embedSWF(\"/swf/cu3er.swf\", \"cu3er_swf\", \"" + width + "\", \"" + height + "\", \"9\", flashvars, params, attributes)\r\n");
            strTemp.Append("</script>\r\n");
            strTemp.Append("<div id=\"cu3er_swf\">\r\n");
            strTemp.Append("</div>\r\n");
            return strTemp.ToString();
        }

        /// <summary>
        /// Kinslideshow焦点图HTML
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string KinslideshowHtml(string width, string height, string target, DataTable dt)
        {
            StringBuilder strTemp = new StringBuilder();

            strTemp.Append("<script src=\"/Js/jquery.KinSlideshow-1.2.1.min.js\" type=\"text/javascript\"></script>\r\n");
            strTemp.Append("<script type=\"text/javascript\">\r\n");
            strTemp.Append("$(function () {\r\n");
            strTemp.Append("$(\"#KinSlideshow\").KinSlideshow()\r\n");
            strTemp.Append(" })\r\n");
            strTemp.Append("</script>\r\n");
            strTemp.Append("<div id=\"KinSlideshow\" style=\"visibility: hidden;width:" + width + "px; height:" + height + "px; overflow:hidden\">\r\n");
            foreach (DataRow row in dt.Rows)
            {
                strTemp.Append("<a href=\"" + row["link_url"] + "\" target=\"" + target + "\">\r\n");
                strTemp.Append("<img src=\"" + row["img_url"] + "\" alt=\"\" width=\"" + width + "\" height=\"" + height + "\" /></a>\r\n");
            }
            strTemp.Append("</div>\r\n");
            return strTemp.ToString();
        }

        public string IscrollHtml(string width, string height, string target, DataTable dt)
        {
            StringBuilder strTemp = new StringBuilder();

            strTemp.Append("<link href=\"/plug/iscroll/iscroll.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n");
            strTemp.Append("<script src=\"/plug/iscroll/iscroll.js\" type=\"text/javascript\"></script>\r\n");
            strTemp.Append("<script type=\"text/javascript\">\r\n");
            strTemp.Append("var myScroll;function loaded() {myScroll = new iScroll('wrapper', {snap: true,momentum: false,hScrollbar: false,onScrollEnd: function () {document.querySelector('#indicator > li.active').className = '';document.querySelector('#indicator > li:nth-child(' + (this.currPageX + 1) + ')').className = 'active';}});}\r\n");
            strTemp.Append("document.addEventListener('DOMContentLoaded', loaded, false);\r\n");
            strTemp.Append("</script>\r\n");

            strTemp.Append("<div id=\"wrapper\"><div id=\"scroller\"><ul id=\"thelist\">\r\n");
            foreach (DataRow row in dt.Rows)
            {
                strTemp.AppendFormat("<li><p>{0}</p><a href=\"{1}\" target=\"{2}\"><img src=\"{3}\" width=\"{4}\" /></a></li>\r\n", row["title"], row["link_url"], target, row["img_url"], width);
            }
            strTemp.Append("</ul></div></div>\r\n");
            strTemp.Append("<div id=\"nav\"><div id=\"prev\" onclick=\"myScroll.scrollToPage('prev', 0,400,3);return false\">&larr; prev</div><ul id=\"indicator\">");

            foreach (DataRow row in dt.Rows)
            {
                strTemp.AppendFormat("<li>{0}</li>\r\n", row["title"]);
            }
            strTemp.Append("</ul><div id=\"next\" onclick=\"myScroll.scrollToPage('next', 0);return false\">next &rarr;</div></div><div class=\"clr\"></div>\r\n");
            strTemp.Append("<script>var count = document.getElementById(\"thelist\").getElementsByTagName(\"img\").length;var count2 = document.getElementsByClassName(\"menuimg\").length;for (i = 0; i < count; i++) {document.getElementById(\"thelist\").getElementsByTagName(\"img\").item(i).style.cssText = \" width:\" + document.body.clientWidth + \"px\";}");
            strTemp.Append("document.getElementById(\"scroller\").style.cssText = \" width:\" + document.body.clientWidth * count + \"px\";setInterval(function () { myScroll.scrollToPage('next', 0, 400, count);}, 3500);window.onresize = function () {for (i = 0; i < count; i++) {document.getElementById(\"thelist\").getElementsByTagName(\"img\").item(i).style.cssText = \" width:\" + document.body.clientWidth + \"px\";}document.getElementById(\"scroller\").style.cssText = \" width:\" + document.body.clientWidth * count + \"px\";}\r\n");
            strTemp.Append("</script>");
            return strTemp.ToString();
        }

        /// <summary>
        /// Slider焦点图HTML
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="target"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string SliderHtml(string width, string height, string target, DataTable dt)
        {
            StringBuilder strTemp = new StringBuilder();

            strTemp.Append("<link href=\"/plug/slider/slider.css\" rel=\"stylesheet\" type=\"text/css\" />");
            strTemp.Append("<script src=\"/plug/slider/slider.js\" type=\"text/javascript\"></script>");
            strTemp.Append("<style type=\"text/css\">");
            strTemp.Append("* html .slides{height: " + height + "px;}");
            strTemp.Append(".flexslider{width: " + width + "px;}");
            strTemp.Append("</style>");
            strTemp.Append("<div class=\"slider\">");
            strTemp.Append("<div class=\"flexslider\">");
            strTemp.Append("<ul class=\"slides\">");

            foreach (DataRow row in dt.Rows)
            {
                strTemp.Append("<li>");
                strTemp.Append("<div class=\"img\">");
                strTemp.AppendFormat("<a href=\"{0}\" target=\"{1}\">", row["link_url"], target);
                strTemp.AppendFormat("<img src=\"{0}\"  width=\"{1}\"  height=\"{2}\" /></a></div>", row["img_url"], width, height);
                strTemp.Append("</li>");
            }
            strTemp.Append("</ul>");
            strTemp.Append("</div>");
            strTemp.Append("</div>");
            strTemp.Append("<script type=\"text/javascript\">");
            strTemp.Append("$(function () {");
            strTemp.Append("$('.flexslider').flexslider({");
            strTemp.Append("animation: \"slide\",");
            strTemp.Append("start: function (slider) {");
            strTemp.Append("$('body').removeClass('loading');");
            strTemp.Append("}");
            strTemp.Append("});");
            strTemp.Append("});");
            strTemp.Append("</script>");
            return strTemp.ToString();
        }

        /// <summary>
        /// KindMaxShow背景100%轮播效果
        /// </summary>
        /// <param name="height"></param>
        /// <param name="target"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string KinmaxshowHtml(string height, string target, DataTable dt)
        {
            StringBuilder strTemp = new StringBuilder();
            strTemp.Append("<script src=\"/plug/kinMaxShow/js/jquery.kinMaxShow-1.0.min.js\" type=\"text/javascript\"></script>");
            strTemp.Append("<script type=\"text/javascript\">");
            strTemp.Append("$(document).ready(function () {");
            strTemp.Append("$(\"#kinMaxShow\").kinMaxShow({");
            strTemp.AppendFormat("height: {0},", height);
            strTemp.Append("button: {");
            strTemp.Append("showIndex: false,");
            strTemp.Append("normal: { background: 'url(/plug/kinMaxShow/img/button.png) no-repeat -14px 0', marginRight: '8px', border: '0', right: '48%', bottom: '20px' },");
            strTemp.Append("focus: { background: 'url(/plug/kinMaxShow/img/button.png) no-repeat 0 0', border: '0' }");
            strTemp.Append("}");
            strTemp.Append("});");
            strTemp.Append("});");
            strTemp.Append("</script>");
            strTemp.Append("<div class=\"banner_box\">");
            strTemp.Append("<div id=\"kinMaxShow\" style=\"display: none;\">");
            foreach (DataRow row in dt.Rows)
            {
                strTemp.Append("<div>");
                strTemp.AppendFormat("<a href=\"{0}\" target=\"{1}\">", row["link_url"], target);
                strTemp.AppendFormat("<img src=\"{0}\" /></a></div>", row["img_url"]);
            }
            strTemp.Append("</div>");
            strTemp.Append("</div>");
            return strTemp.ToString();
        }

        /// <summary>
        /// 单图效果
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="target"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string SingImgHtml(string width, string height, string target, DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                return string.Format("<a href=\"{0}\" target=\"{1}\"><img src=\"{2}\" width=\"{3}\" height=\"{4}\"></a>", dt.Rows[0]["link_url"], target, dt.Rows[0]["img_url"], width, height);
            }
            return string.Empty;
        }

        /// <summary>
        /// 根据语言版本判断是否存在广告位
        /// </summary>
        /// <param name="lang"></param>
        /// <returns></returns>
        public bool IsExistAdPos(string lang)
        {
            BLL.ad_position bll = new BLL.ad_position();
            if (bll.GetRecordCount(string.Format("lang='{0}' and ad_status={1} ", lang, (int)Enums.Status.开启)) > 0)
            {
                return true;
            }
            return false;
        }
    }
}
